package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.Address;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.IAddressMapService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddressMapController {

    @Reference
    private IAddressMapService addressMapService;

    // 添加功能
    @RequestMapping("/add")
    public Result add(@RequestBody Address address){
        try {
            addressMapService.add(address);
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

    // 查询列表
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = addressMapService.findPageByCondition(queryPageBean);
        return pageResult;
    }

    // 删除功能
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            addressMapService.delete(id);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }

    // 查询所有功能
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Address> list = addressMapService.findAll();
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }
}
